package com.qiwenshare.ufop.factory;

import com.github.tobato.fastdfs.FdfsClientConfig;
import com.qiwenshare.ufop.cache.CacheService;
import com.qiwenshare.ufop.cache.impl.CacheServiceJDKImpl;
import com.qiwenshare.ufop.cache.impl.CacheServiceRedisImpl;
import com.qiwenshare.ufop.lock.LockService;
import com.qiwenshare.ufop.lock.impl.LockServiceJDKImpl;
import com.qiwenshare.ufop.lock.impl.LockServiceRedisImpl;
import com.qiwenshare.ufop.operation.copy.product.FastDFSCopier;
import com.qiwenshare.ufop.operation.delete.product.FastDFSDeleter;
import com.qiwenshare.ufop.operation.download.product.FastDFSDownloader;
import com.qiwenshare.ufop.operation.preview.product.FastDFSPreviewer;
import com.qiwenshare.ufop.operation.read.product.FastDFSReader;
import com.qiwenshare.ufop.operation.upload.product.*;
import com.qiwenshare.ufop.operation.write.product.FastDFSWriter;
import com.qiwenshare.ufop.provider.config.CacheConfig;
import com.qiwenshare.ufop.provider.util.UFOPUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;


@Slf4j
@Configuration
//@ConditionalOnClass(UFOService.class)
@EnableConfigurationProperties({UFOPProperties.class})
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class UFOPAutoConfiguration {

    private final UFOPProperties ufopProperties;

    public UFOPAutoConfiguration(UFOPProperties ufopProperties) {
        this.ufopProperties = ufopProperties;
    }

    @Bean
    public UFOPFactory ufopFactory() {
        UFOPUtils.LOCAL_STORAGE_PATH = ufopProperties.getLocalStoragePath();
        String bucketName = ufopProperties.getBucketName();
        if (StringUtils.isNotEmpty(bucketName)) {
            UFOPUtils.ROOT_PATH = ufopProperties.getBucketName();
        } else {
            UFOPUtils.ROOT_PATH = "upload";
        }
        return new UFOPFactory(ufopProperties);
    }

    @Bean
    public FastDFSCopier fastDFSCreater() {
        return new FastDFSCopier();
    }

    @Bean
    public FastDFSUploader fastDFSUploader() {
        return new FastDFSUploader();
    }

    @Bean
    public FastDFSDownloader fastDFSDownloader() {
        return new FastDFSDownloader();
    }

    @Bean
    public FastDFSDeleter fastDFSDeleter() {
        return new FastDFSDeleter();
    }

    @Bean
    public FastDFSReader fastDFSReader() {
        return new FastDFSReader();
    }

    @Bean
    public FastDFSWriter fastDFSWriter() {
        return new FastDFSWriter();
    }

    @Bean
    public FastDFSPreviewer fastDFSPreviewer() {
        return new FastDFSPreviewer(ufopProperties.getThumbImage());
    }

    @Bean
    public AliyunOSSUploader aliyunOSSUploader() {
        return new AliyunOSSUploader(ufopProperties.getAliyun());
    }

    @Bean
    public MinioUploader minioUploader() {
        return new MinioUploader(ufopProperties.getMinio());
    }

    @Bean
    public QiniuyunKodoUploader qiniuyunKodoUploader() {
        return new QiniuyunKodoUploader(ufopProperties.getQiniuyun());
    }

    @Bean
    public CacheService cacheService() {
        CacheConfig cache = ufopProperties.getCache();
        if (cache != null) {
            String type = cache.getType();
            if ("redis".equals(type)) {
                return new CacheServiceRedisImpl();
            } else {
                return new CacheServiceJDKImpl();
            }
        } else {
            return new CacheServiceJDKImpl();
        }

    }

    @Bean
    public LockService lockService() {
        CacheConfig cache = ufopProperties.getCache();
        if (cache != null) {
            String type = cache.getType();
            if ("redis".equals(type)) {
                return new LockServiceRedisImpl();
            } else {
                return new LockServiceJDKImpl();
            }
        } else {
            return new LockServiceJDKImpl();
        }

    }

    @Bean
    public TencentCOSUploader tencentCOSUploader() {
        return new TencentCOSUploader(ufopProperties.getTencent());
    }
}
